package myapp.com.xm.myapplication.View;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;

@SuppressLint("AppCompatCustomView")
public class FllowView extends TextView {
    private float x;
    private float y;
    private float startX;
    private float startY;

    public FllowView(Context context) {
        this(context, null);
    }

    public FllowView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FllowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch ((event.getAction())) {
            case MotionEvent.ACTION_DOWN:
                //获取按下时候的x，y
                x = event.getRawX();
                y = event.getRawY();
                Rect rect = new Rect();
                this.getGlobalVisibleRect(rect);
                startX = rect.left;
                //获取状态栏高度
                Rect rect1 = new Rect();
                ((Activity) getContext()).getWindow().getDecorView().getWindowVisibleDisplayFrame(rect1);
                startY = rect.top - rect1.top;
                break;
            case MotionEvent.ACTION_MOVE:
                this.setTranslationX(startX + event.getRawX() - x);
                this.setTranslationY(startY + event.getRawY() - y);
                break;

            case MotionEvent.ACTION_UP:
                break;
        }
        return true;
    }
}
